package jspservlet.servlet;


import java.util.ArrayList;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;




import jspservlet.vo.User;
import jspservlet.vo.Window;
import jspservlet.vo.Door;
import jspservlet.vo.Lamp;
import jspservlet.vo.Safety;
import jspservlet.vo.Humidity;
import jspservlet.vo.AutoSunVisor;
import jspservlet.vo.Family;
import jspservlet.vo.Fan;
import jspservlet.vo.FireSafety;
import jspservlet.vo.Temperature;

import jspservlet.dao.DeviceDAO;
import jspservlet.dao.UserDAO;

import jspservlet.dao.impl.DeviceDAOImpl;
import jspservlet.dao.impl.UserDAOImpl;


public class LoginServlet extends HttpServlet {
	
	 public void doGet(HttpServletRequest req, HttpServletResponse res)
	    throws IOException, ServletException{
	 }
	
	 public void doPost(HttpServletRequest req, HttpServletResponse res)
	    throws IOException, ServletException{
		HttpSession session=req.getSession();   
		ServletContext application = this.getServletContext();
		 User user = new User();
		 user.setUsername(req.getParameter("phone"));
		 user.setPassword(req.getParameter("password"));
		 UserDAO dao = new UserDAOImpl();   

	     int flag = 0;
	     try {
				flag = dao.login(user);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		} 
		 if(flag == 1){   
	         application.setAttribute("username", user.getUsername());   

			//接showconditionservlet
	    } else {
			PrintWriter out = res.getWriter();
			res.getWriter().println("<script>alert('Invalid Login!')</script>");
			res.getWriter().println("<script>window.location.href='./index.jsp'</script>");
	    }
	 }
}
	 